package com.tsd.jxc.utils;

import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.datasource.SpringContextUtil;
import com.tsd.core.utils.FieldUtils;
import com.tsd.core.utils.HlpUtils;
import com.tsd.jxc.setting.service.JxcSettingService;
import com.tsd.jxc.vo.JxcSettingVo;

/**
 * @ClassName: JxcAutoApprovalUtil
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2022/11/17 18:11
 * @Version: 1.0
 */
public class JxcAutoApprovalUtil {

    public static final String FIELD_AUTO_APPROVAL_SALE_ORDER = "auto_approval_sale_order";
    public static final String FIELD_AUTO_APPROVAL_BUY_ORDER = "auto_approval_buy_order";
    public static final String FIELD_AUTO_GENERATE_BUY_INVOICE = "auto_generate_buy_invoice";

    /**
     * 判断自动审核
     *
     * @param field    对应配置字段
     * @param callback 启用自动审核执行方法
     * @throws Exception
     */
    public static void autoApproval(String field, AutoApprovalCallback callback) throws Exception {
        String accountSetSid = DataSourceContextHolder.getDataSourceType();
        JxcSettingService jxcSettingService = SpringContextUtil.getBean(JxcSettingService.class);
        JxcSettingVo settingVo = jxcSettingService.getJxcSettingVo(accountSetSid);
        if (settingVo == null) {
            return;
        }
        Object fieldValue = FieldUtils.getFieldValue(settingVo, field);
        if (HlpUtils.isEmpty(fieldValue)) {
            return;
        }
        if (JxcSettingVo.STATE_ENABLE == Integer.parseInt(fieldValue.toString())) {
            callback.success();
        }
    }
}
